package com.hongyi.controller;

import com.hongyi.common.Result;
import com.hongyi.pojo.PageBean;
import com.hongyi.pojo.Student;
import com.hongyi.service.StudentService;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * @author hongyi
 * @date 2023/11/25 11:05
 */
@RestController
@CrossOrigin
@MapperScan("com.hongyi.mapper")

@RequestMapping("/student")
public class StudentController {
    @Autowired
    private StudentService studentService;

    @PostMapping("/add")
    public Result add(@RequestBody Student student) {
        studentService.add(student);
        return Result.success("添加成功");
    }

    @PutMapping("/update")
    public Result update(@RequestBody Student student) {
        studentService.update(student);
        return Result.success("修改成功");
    }

    @DeleteMapping("/{id}")
    public Result delete(@PathVariable Integer id) {
        studentService.delete(id);
        return Result.success("删除成功");
    }

    @GetMapping("/{id}")
    public Result findById(@PathVariable Integer id) {
        Student finById = studentService.finById(id);
//        Result success = Result.success(finById);
        return Result.success("查询成功");
    }

    @GetMapping("/page")
    public PageBean page( @RequestParam("currentPage") int currentPage,
                          @RequestParam("pageSize") int pageSize,
                          @RequestParam("name") String name,
                          @RequestParam("sex") String sex  ) {
        PageBean<Student> page = studentService.selectPage(name, sex, currentPage, pageSize);
        return page;
    }


}
